package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"product-mall/models/mysql"

	"product-mall/productrpc/internal/svc"
	"product-mall/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateClassLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateClassLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateClassLogic {
	return &UpdateClassLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateClassLogic) UpdateClass(in *productrpc.UpdateClassRequest) (*productrpc.UpdateClassResponse, error) {
	// todo: add your logic here and delete this line
	var class mysql.Class
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&class)
	if class.UserId != in.UserId {
		return nil, fmt.Errorf("权限不足")
	}

	class = mysql.Class{
		Model: gorm.Model{ID: uint(in.Id)},
		Name:  in.Name,
	}
	err := mysql.DB.Debug().Updates(&class).Error
	if err != nil {
		return nil, fmt.Errorf("商品修改失败")
	}

	return &productrpc.UpdateClassResponse{Id: int64(class.ID)}, nil
}
